import random

def gen_key():
    chars = []
    for i in range(8):
        chars.append(random.choice("0123456789BCDFGHJKLMNPQRSTVWXZ"))
    return "".join(chars)

class Registry(dict):
    def gen_key(self):
        """Continues to generate keys until it finds one we haven't used yet"""
        while 1:
            key = gen_key()
            if key not in self:
                return key
    def add(self, object, key=None):
        """Adds an object to the registry."""
        if key is None:
            key = self.gen_key()
        self[key] = object
        object.key = key
        return object
    def delete(self,key):
        """Deletes object belonging to key, if it exists"""
        if key in self:
            ob = self[key]
            del self[key]
            return ob
        return None
    def all(self):
        """Returns a list of all the objects for easy iteration"""
        return self.values()
    def get_state(self):
        """Serialize object state into a dictionary"""
        d = {}
        for k in self:
            o = self[k]
            if hasattr(o,"get_state"):
                d[k] = o.get_state()
        return d